<script setup lang="ts">
import type {
  CodeRuleConfigQo,
} from '#/system/codeConfig'
import {
  createCodeRuleConfigApi,
  getCodeRuleConfigDetailsApi,
  updateCodeRuleConfigApi,
} from '@/api/system/codeConfig'

interface OnCloseResolveType extends CloseResolveType {}
let onCloseResolve = function (_value: OnCloseResolveType) { }

const dialogRef = $ref<DialogInstance>()
const ruleFormRef = ref<FormInstance>()
let dialogModal = $ref<CodeRuleConfigQo>({})
let dialogAction = $ref<DialogAction>('add')
let confirmLoading = $ref(false)
let dialogLoading = $ref(false)

const rules = reactive<FormRules<CodeRuleConfigQo>>({
  ruleKey: [
    { required: true, message: '请输入规则标识', trigger: 'blur' },
  ],
  ruleName: [
    { required: true, message: '请输入规则名称', trigger: 'blur' },
  ],
  dateFormat: [
    { required: true, message: '请输入日期格式', trigger: 'blur' },
  ],
  seqType: [
    { required: true, message: '请选择序号类型', trigger: 'blur' },
  ],
  seqLength: [
    { required: true, message: '请输入序号长度', trigger: 'blur' },
  ],
})
async function getDefaultModel() {
  return {
    enableCache: 1,
    seqType: 1,
    prefix: '',
    suffix: '',
  } as CodeRuleConfigQo
}
// 打开
async function showDialog(args: {
  title: string
  action?: DialogAction
  model: {
    id?: string
  }
}) {
  const dialogRes = dialogRef!.openDialog<OnCloseResolveType>({
    title: args.title,
  })

  ruleFormRef.value?.clearValidate()
  onCloseResolve = dialogRes.onCloseResolve

  dialogAction = args.action ?? 'add'

  if (args.model.id) {
    dialogModal.id = args.model.id
  }

  if (args.action === 'add') {
    const defaultModel = await getDefaultModel()
    dialogModal = {
      ...defaultModel,
      ...args.model,
    }
  }
  else if (args.action === 'edit' || args.action === 'views') {
    await reflash()
  }

  return await dialogRes.dialogPrm
}

async function reflash() {
  if (!dialogModal.id)
    return
  dialogLoading = true
  const res = await getCodeRuleConfigDetailsApi(dialogModal.id)
  if (!res)
    return
  dialogLoading = false
  Object.assign(dialogModal, res.data)
}

// 确认
async function confirm(formEl: FormInstance | undefined) {
  if (!formEl)
    return
  await formEl.validate(async (valid) => {
    if (!valid) {
      return
    }
    confirmLoading = true
    if (dialogAction === 'add') {
      await createCodeRuleConfigApi(dialogModal).finally(() => {
        confirmLoading = false
      })
    }
    else if (dialogAction === 'edit') {
      await updateCodeRuleConfigApi(dialogModal).finally(() => {
        confirmLoading = false
      })
    }
    confirmLoading = false
    Message({
      type: 'success',
      message: `${DialogActionMap[dialogAction as DialogAction]}成功`,
    })
    onCloseResolve({ type: true })
  })
}

// 取消
function cancel() {
  ruleFormRef.value?.clearValidate()
  onCloseResolve({ type: false })
}

defineExpose({ showDialog })
</script>

<template>
  <sy-dialog
    ref="dialogRef"
    :before-close="cancel"
    :hide-confirm="dialogAction === 'views'"
    :confirm-loading="confirmLoading"
    :disabled-confirm="dialogLoading"
    @confirm="confirm(ruleFormRef)"
  >
    <el-form
      ref="ruleFormRef"
      v-loading="dialogLoading"
      :model="dialogModal"
      :rules="rules"
      label-width="auto"
      status-icon
    >
      <el-form-item label="规则标识" prop="ruleKey">
        <el-input
          v-model="dialogModal.ruleKey"
          placeholder="请输入规则标识"
          :disabled="dialogAction === 'views'"
        />
      </el-form-item>
      <el-form-item label="规则名称" prop="ruleName">
        <el-input
          v-model="dialogModal.ruleName"
          placeholder="请输入规则名称"
          :disabled="dialogAction === 'views'"
        />
      </el-form-item>
      <el-form-item label="固定前缀" prop="prefix">
        <el-input
          v-model="dialogModal.prefix"
          placeholder="请输入固定前缀"
          :disabled="dialogAction === 'views'"
        />
      </el-form-item>
      <el-form-item label="日期格式" prop="dateFormat">
        <el-input
          v-model="dialogModal.dateFormat"
          placeholder="请输入日期格式"
          :disabled="dialogAction === 'views'"
        />
      </el-form-item>
      <el-form-item label="序号类型" prop="seqType">
        <sy-dict
          v-model="dialogModal.seqType"
          dict-type="code_config_seq_type"
          placeholder="请选择序号类型"
          :disabled="dialogAction === 'views'"
        />
      </el-form-item>
      <el-form-item label="序号长度" prop="seqLength">
        <el-input
          v-model="dialogModal.seqLength"
          type="number"
          placeholder="请输入序号长度"
          :disabled="dialogAction === 'views'"
        />
      </el-form-item>
      <el-form-item label="固定后缀" prop="suffix">
        <el-input
          v-model="dialogModal.suffix"
          placeholder="请输入固定后缀"
          :disabled="dialogAction === 'views'"
        />
      </el-form-item>
      <el-form-item label="分隔符" prop="delimiter">
        <el-input
          v-model="dialogModal.delimiter"
          placeholder="请输入分隔符"
          :disabled="dialogAction === 'views'"
        />
      </el-form-item>
      <el-form-item label="是否缓存" prop="enableCache">
        <sy-dict
          v-model="dialogModal.enableCache"
          dict-type="enable_status"
          :disabled="dialogAction === 'views'"
          type="segmented"
        />
      </el-form-item>

      <el-form-item label="备注" prop="description">
        <el-input
          v-model="dialogModal.description"
          type="textarea"
          placeholder="请输入备注"
          :disabled="dialogAction === 'views'"
        />
      </el-form-item>
    </el-form>
  </sy-dialog>
</template>

<style scoped></style>
